﻿/**************************************************************************
 * Copyright 2013 Arjen van Efferen / arjenvanefferen@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *************************************************************************/
using System;
using System.Globalization;

namespace CommonUtils
{
    /// <summary>
    /// Assert helper functions
    /// </summary>
	public class Assert
	{
        /// <summary>
        /// If given object with parameter name is null an NullReferenceException is thrown
        /// </summary>
        /// <param name="property">The object to check for NULL</param>
        /// <param name="name">The name of the parameter</param>
		public static void PropertyNotNull(object property, string name)
		{
			Assert.PropertyNotNull(property, name, string.Format(CultureInfo.InvariantCulture, "[Assertion failed] - Property '{0}' cannot be null.", name));
		}

		public static void PropertyNotNull(object property, string name, string message)
		{
			if (property == null)
				throw new NullReferenceException(message);
		}

		public static void ArgumentNotNull(object argument, string name)
		{
			Assert.ArgumentNotNull(argument, name, string.Format(CultureInfo.InvariantCulture, "[Assertion failed] - Argument '{0}' cannot be null.", name));
		}

		public static void ArgumentNotNull(object argument, string name, string message)
		{
			if (argument == null)
				throw new ArgumentNullException(name, message);
		}

		public static void ArgumentHasText(string argument, string name)
		{
			Assert.ArgumentHasText(argument, name, string.Format(CultureInfo.InvariantCulture, "[Assertion failed] - Argument '{0}' cannot be null or resolve to an empty string : '{1}'.", name, argument));
		}

		public static void ArgumentHasText(string argument, string name, string message)
		{
			if (String.IsNullOrEmpty(argument))
				throw new ArgumentNullException(name, message);
		}

		public static void AssertArgumentType(object argument, string argumentName, Type requiredType)
		{
			Assert.AssertArgumentType(argument, argumentName, requiredType, string.Format("[Assertion failed] - Argument {0} is not of the correct type.", argumentName));
		}

		public static void AssertArgumentType(object argument, string argumentName, Type requiredType, string message)
		{
			if (argument != null && requiredType != null && !requiredType.IsAssignableFrom(argument.GetType()))
				throw new ArgumentException(message, argumentName);
		}

		public static void IsTrue(bool expression, string message)
		{
			if (!expression)
				throw new ArgumentException(message);
		}

		public static void IsTrue(bool expression)
		{
			IsTrue(expression, "[Assertion failed] - this expression must be true");
		}

		public static void IsFalse(bool expression, string message)
		{
			if (expression)
				throw new ArgumentException(message);
		}

		public static void IsFalse(bool expression)
		{
			IsFalse(expression, "[Assertion failed] - this expression must be false");
		}
	}
}
